package VISTA.ventanas;

import BD.Pagoscliente;
import NEGOCIOS.ControlUniversalBD;
import Utilidades.ManejadorFechas;
import java.beans.PropertyVetoException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import reportes.Modulos.ModuloReporteNoBD;

public class PagosDeClientes extends javax.swing.JInternalFrame {

    double totalDeDinero = 0;

    public PagosDeClientes() {
        initComponents();
        totalDeDinero = 0;
        jtfTotal.setText(String.valueOf(0));
        //Formato de tiempo
        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        jtfFecha.setText(String.valueOf(formato.format(ManejadorFechas.getFechaActual())));
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jlNombreCliente = new javax.swing.JLabel();
        jlFecha = new javax.swing.JLabel();
        jtfFecha = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jbAgregar = new javax.swing.JButton();
        jbQuitar = new javax.swing.JButton();
        jlDinero = new javax.swing.JLabel();
        jtfDinero = new javax.swing.JTextField();
        jlRFC = new javax.swing.JLabel();
        jtfNombre = new javax.swing.JTextField();
        jtfRFC = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtPagos = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jlTotal = new javax.swing.JLabel();
        jbGuardar = new javax.swing.JButton();
        jbImprimir = new javax.swing.JButton();
        jbSalir = new javax.swing.JButton();
        jtfTotal = new javax.swing.JTextField();
        jlTitulo = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Pagos de clientes");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/dollar.png"))); // NOI18N

        jPanel2.setBackground(new java.awt.Color(153, 51, 0));

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));

        jlNombreCliente.setBackground(new java.awt.Color(102, 102, 102));
        jlNombreCliente.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlNombreCliente.setText("Nombre del cliente:");

        jlFecha.setBackground(new java.awt.Color(102, 102, 102));
        jlFecha.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlFecha.setText("Fecha: ");

        jtfFecha.setEditable(false);
        jtfFecha.setBackground(new java.awt.Color(255, 255, 255));
        jtfFecha.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jtfFecha.setForeground(new java.awt.Color(102, 0, 0));

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));

        jbAgregar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jbAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/mas.png"))); // NOI18N
        jbAgregar.setText("Agregar");
        jbAgregar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAgregarActionPerformed(evt);
            }
        });

        jbQuitar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jbQuitar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/menos.png"))); // NOI18N
        jbQuitar.setText("Quitar");
        jbQuitar.setEnabled(false);
        jbQuitar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbQuitar.setPreferredSize(new java.awt.Dimension(97, 41));
        jbQuitar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbQuitarActionPerformed(evt);
            }
        });

        jlDinero.setBackground(new java.awt.Color(102, 102, 102));
        jlDinero.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlDinero.setText("Dinero:");

        jtfDinero.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jtfDinero.setForeground(new java.awt.Color(153, 0, 0));
        jtfDinero.setText("0");
        jtfDinero.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtfDineroMouseClicked(evt);
            }
        });
        jtfDinero.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtfDineroFocusGained(evt);
            }
        });
        jtfDinero.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfDineroKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlDinero)
                .addGap(18, 18, 18)
                .addComponent(jtfDinero, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbQuitar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlDinero)
                    .addComponent(jtfDinero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jlRFC.setBackground(new java.awt.Color(102, 102, 102));
        jlRFC.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlRFC.setText("RFC:");

        jtfNombre.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jtfNombre.setForeground(new java.awt.Color(153, 0, 0));
        jtfNombre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtfNombreMouseClicked(evt);
            }
        });
        jtfNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jtfNombreFocusGained(evt);
            }
        });
        jtfNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfNombreKeyTyped(evt);
            }
        });

        jtfRFC.setBackground(new java.awt.Color(255, 255, 255));
        jtfRFC.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jtfRFC.setForeground(new java.awt.Color(102, 0, 0));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlNombreCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jlFecha)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlRFC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jtfRFC, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jtfNombre))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jlRFC)
                        .addComponent(jtfRFC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jlFecha)
                        .addComponent(jtfFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jlNombreCliente)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(3, 3, 3)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        jtPagos.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jtPagos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cliente", "Dinero"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jtPagos);

        jPanel6.setBackground(new java.awt.Color(255, 255, 255));

        jlTotal.setBackground(new java.awt.Color(102, 102, 102));
        jlTotal.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlTotal.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlTotal.setText("Total:");

        jbGuardar.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jbGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/save.png"))); // NOI18N
        jbGuardar.setText("Guardar");
        jbGuardar.setEnabled(false);
        jbGuardar.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbGuardarActionPerformed(evt);
            }
        });

        jbImprimir.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jbImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/printer.png"))); // NOI18N
        jbImprimir.setText("Imprimir");
        jbImprimir.setEnabled(false);
        jbImprimir.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbImprimirActionPerformed(evt);
            }
        });

        jbSalir.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jbSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/VISTA/iconos/general/Cancel.png"))); // NOI18N
        jbSalir.setText("Salir");
        jbSalir.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jbSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSalirActionPerformed(evt);
            }
        });

        jtfTotal.setEditable(false);
        jtfTotal.setBackground(new java.awt.Color(255, 255, 255));
        jtfTotal.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jtfTotal.setForeground(new java.awt.Color(153, 51, 0));
        jtfTotal.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        jtfTotal.setText("0.00");
        jtfTotal.setToolTipText("");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jbGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jlTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbGuardar, jbImprimir, jbSalir});

        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jbSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jbImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jlTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel6Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jbGuardar, jbImprimir, jbSalir, jlTotal, jtfTotal});

        jlTitulo.setBackground(new java.awt.Color(255, 204, 0));
        jlTitulo.setFont(new java.awt.Font("Brush Script MT", 3, 36)); // NOI18N
        jlTitulo.setText(" Pagos de clientes");
        jlTitulo.setOpaque(true);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addContainerGap())
            .addComponent(jlTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jlTitulo, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbAgregarActionPerformed
        //Revisa que los campos esten llenos
        if (!(jtfNombre.getText().equals("") || jtfDinero.getText().equals(""))) {
            //Obtengo el modelo para manipular sus datos
            DefaultTableModel model = (DefaultTableModel) jtPagos.getModel();
            try {
                //Pasa el texto en jtfDinero a la variable dinero
                double dinero = Double.parseDouble(jtfDinero.getText());
                //Comprueba que la informacion sea correcta
                if (dinero > 0) {
                    //Creo una fila para el modelo y la agrego
                    model.addRow(new Object[]{jtfNombre.getText(), dinero});
                    habilitarBotones();
                    //Actualizo el valor del total
                    generarTotal(dinero);
                } else {
                    JOptionPane.showMessageDialog(this,
                            "la cantidad de dinero debe ser mayor a 0",
                            "Atencion!", 1);
                }
                //Limpia los campos para añadir mas filas
                limpiarCampos();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, e, "Error", 0);
            }
        } else {
            JOptionPane.showMessageDialog(this, "Faltan datos", "Atencion!", 1);
        }
    }//GEN-LAST:event_jbAgregarActionPerformed

    private void generarTotal(Double totalPago) {
        //Genera el total de todos los pagos 
        Double total = Double.parseDouble(jtfTotal.getText().toString());
        total += totalPago;
        jtfTotal.setText(total.toString());
    }

    private void restarTotal(Double totalPago) {
        //Genera el total de todos los pagos 
        Double total = Double.parseDouble(jtfTotal.getText());
        total -= totalPago;
        jtfTotal.setText(total.toString());
    }

    private void jbQuitarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbQuitarActionPerformed
        //Revisa si hay una fila seleccionada
        if (jtPagos.getSelectedRow() > -1) {
            //Obtengo el modelo para manipular sus datos
            DefaultTableModel model = (DefaultTableModel) jtPagos.getModel();
            //Actualizo el valor del total
            restarTotal(
                    Double.parseDouble(
                            jtPagos.getValueAt(
                                    jtPagos.getSelectedRow(), 1).toString()
                    )
            );
            //Remueve la fila del modelo
            model.removeRow(jtPagos.getSelectedRow());
            //Revisa si aun hay filas
            if (jtPagos.getRowCount() < 1) {
                //Deshabilita el boton de quitar
                deshabilitarBotones();
            }
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un producto de la "
                    + "tabla para poder borrarlo.", "Producto no seleccionado",
                    0);
        }
    }//GEN-LAST:event_jbQuitarActionPerformed

    private void jbSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSalirActionPerformed
        try {
            this.setClosed(true);
            System.gc();
        } catch (PropertyVetoException ex) {
            this.dispose();
            System.gc();
        }
    }//GEN-LAST:event_jbSalirActionPerformed

    private void jbGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbGuardarActionPerformed
        //Revisa si hay una fila seleccionada
        if (jtPagos.getRowCount() > 0) {
            //ControlPagoClientes cpc = new ControlPagoClientes();
            ControlUniversalBD cpc = new ControlUniversalBD();
            //Guarda los valores de la tabla en la base de datos
            for (int i = 0; i < jtPagos.getRowCount(); i++) {
                try {
                    //Generamos el nuevo objeto que se agregara a la lista
                    Pagoscliente pc = new Pagoscliente();
                    pc.setFecha((Date) ManejadorFechas.getFechaActual());//Fecha
                    pc.setNombre((String) jtPagos.getValueAt(i, 0)); //Nombre
                    pc.setTotal((double) jtPagos.getValueAt(i, 1)); //Dinero
                    cpc.add(pc);
                    jtfTotal.setText("");
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, "Error:" + "\n" + ex,
                            "Error al intentar guardar el pago", 0);
                }
            }
            limpiarCampos();
            JOptionPane.showMessageDialog(this, "Registros guardados "
                    + "correctamente Guardado");
        } else {
            JOptionPane.showMessageDialog(this, "No hay registros en la tabla "
                    + "para guardar", "Sin registros",
                    0);
        }
        //Llamo el metodo que borra el contenido de la tabla
        borrarContenidoTabla();
    }//GEN-LAST:event_jbGuardarActionPerformed
    private void borrarContenidoTabla() {
        //Obtengo el modelo
        DefaultTableModel modelo = (DefaultTableModel) jtPagos.getModel();
        //Borro las todas las rows
        while (modelo.getRowCount() > 0) {
            modelo.removeRow(0);
        }
        deshabilitarBotones();
    }
    private void jtfDineroKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDineroKeyTyped
        char key = evt.getKeyChar();
        if ((key < '0' || key > '9') && (key != '\b') && (key != '\n')) {
            evt.consume();
            JOptionPane.showMessageDialog(this, "Solo se permiten numeros en "
                    + "este campo", "Letras no permitidas", 0);
        }
    }//GEN-LAST:event_jtfDineroKeyTyped

    private void jtfDineroMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtfDineroMouseClicked
        jtfDinero.setText("");
    }//GEN-LAST:event_jtfDineroMouseClicked

    private void jtfDineroFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtfDineroFocusGained
        jtfDinero.setText("");
    }//GEN-LAST:event_jtfDineroFocusGained

    private void jtfNombreMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtfNombreMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfNombreMouseClicked

    private void jtfNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jtfNombreFocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfNombreFocusGained

    private void jtfNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfNombreKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_jtfNombreKeyTyped

    private void jbImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbImprimirActionPerformed
        //Verifico que la tabla no este vacia
        if (jtPagos.getRowCount() > 0) {
            //Si hay registros en la BD genero el reporte
            ModuloReporteNoBD reporteNBD = new ModuloReporteNoBD(jtPagos, "Clientes");
        } else {
            //Si no existen registros en la base de datos lo reporto al usuario
            JOptionPane.showMessageDialog(this, "No hay pagos de clientes"
                    + "existentes." + "\n" + "Introdusca al menos un pago para "
                    + "poder generar el reporte.", "No hay pagos", 0);
        }

    }//GEN-LAST:event_jbImprimirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbAgregar;
    private javax.swing.JButton jbGuardar;
    private javax.swing.JButton jbImprimir;
    private javax.swing.JButton jbQuitar;
    private javax.swing.JButton jbSalir;
    private javax.swing.JLabel jlDinero;
    private javax.swing.JLabel jlFecha;
    private javax.swing.JLabel jlNombreCliente;
    private javax.swing.JLabel jlRFC;
    private javax.swing.JLabel jlTitulo;
    private javax.swing.JLabel jlTotal;
    private javax.swing.JTable jtPagos;
    private javax.swing.JTextField jtfDinero;
    private javax.swing.JTextField jtfFecha;
    private javax.swing.JTextField jtfNombre;
    private javax.swing.JTextField jtfRFC;
    private javax.swing.JTextField jtfTotal;
    // End of variables declaration//GEN-END:variables

    private void limpiarCampos() {
        jtfDinero.setText("");
        jtfNombre.setText("");
    }

    private void deshabilitarBotones() {
        jbQuitar.setEnabled(false);
        jbGuardar.setEnabled(false);
        jbImprimir.setEnabled(false);
    }

    private void habilitarBotones() {
        //activo el boton quitar por que ya tengo filas en la tabla
        jbQuitar.setEnabled(true);
        jbGuardar.setEnabled(true);
        jbImprimir.setEnabled(true);
    }

}
